<?php

namespace App\Http\Controllers;

use Illuminate\Database\QueryException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

use Illuminate\Support\Facades\DB;
use App\Tools\Func;
use App\Models\User;
use App\Models\Jiuzhenren;

class UserController extends Controller
{
	use Func;
	
	public function wxLogin(Request $request)
	{
		// 微信 wx.login 发放调用
		$url = 'https://api.weixin.qq.com/sns/jscode2session';
        $client = new \GuzzleHttp\Client(['timeout' =>30, 'verify' => false]);
        $response = $response = $client->request('get', $url, [
            'query' => [
				'appid' => config('wx.appid'),
				'secret' => config('wx.secret'),
				'js_code' => $request->code,
				'grant_type' => 'authorization_code'
			]
        ]);

        $result = json_decode($response->getBody(), true); 
		$user = User::firstOrCreate([
			'openid' => $result['openid']
		]);
        // dump();
		//$this->getAccessToken();
		
		return $this->returnJson(0, 'success', $user); 
	}
	


	

}
